package cn.lwj.webService.util;

import com.thoughtworks.xstream.converters.ConversionException;
import com.thoughtworks.xstream.converters.Converter;
import com.thoughtworks.xstream.converters.MarshallingContext;
import com.thoughtworks.xstream.converters.UnmarshallingContext;
import com.thoughtworks.xstream.io.HierarchicalStreamReader;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;

public class IntegerConverter implements Converter {
	
	public boolean canConvert(Class clazz) {
		return Integer.class.isAssignableFrom(clazz);

	}

	public void marshal(Object value, HierarchicalStreamWriter writer,MarshallingContext context) {
		if(value!=null){
			writer.setValue(value.toString());
		}
	}

	public Object unmarshal(HierarchicalStreamReader reader,UnmarshallingContext context) {
		Integer intger = null;
		try {
			if(reader.getAttribute("xsi:nil")!=null&&reader.getAttribute("xsi:nil").toString().equals("true")&&(reader.getValue()==null||reader.getValue().trim().equals(""))){
				return null;
			}
			intger = Integer.parseInt(reader.getValue());
		} catch (Exception e) {
			throw new ConversionException(e.getMessage(), e);
		}
		return intger;
	}

}
